﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.PayFactory.Common;

namespace ZHDJ.AppInterface.Controllers
{
    public class MemberCostController : ApiController
    {
        DataTable dt = new DataTable();
        #region 获取党费缴纳实体
        [HttpPost]
        public ReturnResult GetMemberCostModel(dynamic obj)
        {
            string BranchID = Convert.ToString(obj.BranchID);
            string UserName = Convert.ToString(obj.UserName);
            PartyMemberCost model = InterfaceHelper.GetBranchCostModel(BranchID, UserName);
            return new ReturnResult()
            {
                code = "200",
                result = model,
                msg = "success"
            };
        }
        #endregion

        #region 批量添加党费
        [HttpPost]
        public ReturnResult AddMemberCost(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            PartyMemberCost current = new PartyMemberCost();
            try
            {
                string[] monthsArray = Convert.ToString(obj.Months).TrimEnd(',').Split(',');
                string out_trade_no = OrderHelper.GenerateId();
                #region 添加党费缴纳订单
                for (int i = 0; i < monthsArray.Length; i++)
                {
                    current.ID = StringHelper.NewID;
                    current.LoginID = Convert.ToString(obj.LoginID);
                    current.Status = 5;//待付款
                    current.CreateTime = DateTime.Now;
                    current.ModifyTime = DateTime.Now;
                    current.CreateBy = Convert.ToString(obj.LoginID);
                    current.ModifyBy = Convert.ToString(obj.LoginID);
                    current.Month = monthsArray[i];
                    current.PayDate = DateTime.Now;
                    current.PayWay = "2";
                    current.Year = DateTime.Now.Year.ToString();
                    current.Cost = Convert.ToDecimal(obj.CostBase);
                    current.OrderNumber = out_trade_no;
                    InterfaceHelper.AddBranchCost(current);
                }
                #endregion
                result.code = "200";
                result.result = current;
                result.msg = "success";
            }
            catch (Exception ex)
            {
                result.code = "201";
                result.msg = "error";
            }
            return result;
        }
        #endregion

        #region 根据OrderNumber批量修改党费
        [HttpPost]
        public ReturnResult UpdateMemberCostByOrderNumber(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            try
            {
                string OrderNumber = Convert.ToString(obj.OrderNumber);
                dt = InterfaceHelper.GetBranchCostByOrderNumber(OrderNumber);
                foreach (DataRow item in dt.Rows)
                {
                    PartyMemberCost current = new PartyMemberCost();
                    current.ID = item["ID"].ToString();
                    current.ModifyTime = DateTime.Now;
                    current.Status = 4;
                    InterfaceHelper.UpdateBranchCostByID(current);
                }
                result.code = "200";
                result.msg = "success";
            }
            catch (Exception ex)
            {
                result.code = "201";
                result.msg = "error";
            }
            return result;
        }
        #endregion
    }
}
